/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package org.letmeshare.stockkeeper.view.login;

import java.awt.Button;
import java.awt.Component;
import java.awt.Label;
import java.awt.TextField;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import javax.swing.ImageIcon;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JPanel;
import javax.swing.JPasswordField;
import javax.swing.JTextField;
import org.jdesktop.swingx.HorizontalLayout;
import org.jdesktop.swingx.VerticalLayout;
import org.letmeshare.stockkeeper.AppInit;
import org.letmeshare.stockkeeper.component.Horizontalpanel;
import org.letmeshare.stockkeeper.component.ImagePanel;
import org.letmeshare.stockkeeper.view.View;

/**
 *
 * @author abin
 */
public class Loginview extends JFrame implements View {

    private LoginActionListerner loginListener;
    private JTextField userName;
    private JPasswordField password;

    public Loginview() {
        HorizontalLayout horizontalLayout = new HorizontalLayout();
        VerticalLayout verticalLayout = new VerticalLayout();
        JPanel panel = new JPanel(horizontalLayout);
        JPanel loginPanel = new JPanel(verticalLayout);
        JLabel imageJLabel = new JLabel();
        imageJLabel.setIcon(new ImageIcon("pre-load.png"));
        panel.add(imageJLabel);

        Button button = new Button("Login");
        Horizontalpanel buttonHorizontalpanel = new Horizontalpanel();
        buttonHorizontalpanel.add(new Label("                     "));// to allign login button
        buttonHorizontalpanel.add(button);
        button.setSize(10, 20);
        button.addActionListener(new ActionListener() {
            public void actionPerformed(ActionEvent e) {
                onLogin();
            }
        });
        JPanel usernamePanel = new JPanel(horizontalLayout);
        usernamePanel.add(new Label("User Name :"));
        userName = new JTextField(20);
        usernamePanel.add(userName);

        JPanel passwordPanel = new JPanel(horizontalLayout);
        passwordPanel.add(new Label("Password  :"));
        password = new JPasswordField(20);
        passwordPanel.add(password);
        loginPanel.add(new Label(" "));
        loginPanel.add(usernamePanel);
        loginPanel.add(passwordPanel);
        loginPanel.add(buttonHorizontalpanel);
        panel.add(loginPanel);
        this.add(panel);

        setSize(400, 130);
        setVisible(true);
        setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        setResizable(false);
        setIconImage(AppInit.getIcon());
        setTitle("Login : Stock Keeper");
        setLocation(AppInit.GetScreenWorkingWidth() / 2, AppInit.GetScreenWorkingHeight() / 2 - 50);
    }

    public Component getView() {
        return this;
    }

    public void addLoginActionListener(LoginActionListerner loginActionListerner) {
        this.loginListener = loginActionListerner;
    }

    private void onLogin() {
        if (!(loginListener == null)) {
            loginListener.onLoginAction(new LoginEvent(this, 1, null, userName.getText(), password.getPassword()));
        }
    }
}
